//
//  DFProductsProductDataDetailController.m
//  cf
//
//  Created by lanou on 15/10/26.
//  Copyright © 2015年 xuzenghui. All rights reserved.
//

#import "DFProductsProductDataDetailController.h"
#import "DFProductsProductDetailCell.h"
#import "PKRequestManager.h"
#import "DFProductsProductParameterModel.h"
@interface DFProductsProductDataDetailController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong)NSMutableArray *sectionIndexArr;

@end

@implementation DFProductsProductDataDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    _sectionIndexArr = [NSMutableArray array];
    self.dataBaseArr = [NSMutableArray array];
    self.mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 104, kScreenWidth, kScreenHeight - 104) style:UITableViewStylePlain];
    self.mainTableView.delegate = self;
    self.mainTableView.dataSource = self;
    [self.mainTableView registerClass:[DFProductsProductDetailCell class] forCellReuseIdentifier:@"da"];
    
    [self.view addSubview:self.mainTableView];
    [self requestDataWithString:_proId];
    // Do any additional setup after loading the view.
}

- (void)requestDataWithString:(NSString*)proId
{
    NSString *urlString = [@"http://lib3.wap.zol.com.cn/index.php?c=Iphone_37o_ProParam&proId=" stringByAppendingString:proId];
    
    [PKRequestManager requestWithType:GET urlString:urlString parDic:nil finish:^(NSData *data) {
        [self.dataBaseArr removeAllObjects];
        NSArray *arr = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        for (NSDictionary *dic in arr) {
            [_sectionIndexArr addObject:dic[@"name"]];
            DFProductsProductParameterModel *model = [[DFProductsProductParameterModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            
            [self.dataBaseArr addObject:model];
            
            [self.mainTableView reloadData];
        }
    } error:^(NSError *error) {
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataBaseArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[self.dataBaseArr[section] arr] count] ;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DFProductsProductDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"da" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    DFProductsProductsortParam *model = [self.dataBaseArr[indexPath.section] arr][indexPath.row];
    [cell configureDataWithModel:model];
    
    return cell;
}

- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [self.dataBaseArr[section] name];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    DFProductsProductsortParam *model = [self.dataBaseArr[indexPath.section] arr][indexPath.row];

    return [DFProductsProductDetailCell heithtOfCell:model.vale];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
